package com.orm.mybatis.swagger3.controller;

import com.orm.mybatis.swagger3.entity.User;
import com.orm.mybatis.swagger3.serviceImpl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2022-03-11 09:53:09
 */
@Api(tags = "测试例子")
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserServiceImpl userService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value="用户查询", notes="根据ID查询用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path" ,dataTypeClass = Integer.class)
    @GetMapping("{id}")
    public ResponseEntity<User> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.userService.queryById(id));
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @ApiOperation(value = "用户列表",notes = "查询用户列表")
    @GetMapping("/all")
    public ResponseEntity<List<User>> queryAll() {
        return ResponseEntity.ok(userService.queryAll());
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @ApiOperation(value = "添加用户",notes = "创建新用户")
    @ApiImplicitParam(name = "user",value = "用户详细实体user",required = true ,dataType = "User")
    @PostMapping
    public ResponseEntity<User> add(@RequestBody User user) {
        System.out.println(user.toString());
        return ResponseEntity.ok(this.userService.insert(user));
    }

    /**
     * 编辑数据
     *
     * @param user 实体
     * @return 编辑结果
     */
    @ApiOperation(value = "更新用户",notes = "根据用户更新用户信息")
    @ApiImplicitParam(name = "user",value = "用户对象user",required = true,dataType = "User")
    @PutMapping
    public ResponseEntity<User> edit(@RequestBody User user) {
        return ResponseEntity.ok(this.userService.update(user));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation(value = "删除用户",notes = "根据id删除指定用户")
    @ApiImplicitParam(name = "id",value = "用户ID",required = true,dataType = "Long",paramType = "query",dataTypeClass = Long.class)
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.userService.deleteById(id));
    }

}

